package com.auction.exception;

import com.auction.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理业务异常
     * 
     * @param ex
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public Result handleBusinessException(BusinessException ex) {
        log.error("业务异常：{}", ex.getMessage());
        return Result.error(ex.getMessage());
    }

    /**
     * 处理系统异常
     * 
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception ex) {
        log.error("系统异常：", ex);
        return Result.error("系统繁忙，请稍后再试");
    }

    /**
     * 处理404异常
     * 
     * @param ex
     * @return
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public Result<String> noHandlerFoundExceptionHandler(NoHandlerFoundException ex) {
        log.error("资源不存在异常：{}", ex.getMessage());
        return Result.error("请求的资源不存在");
    }
}